package com.ziq.zcamp.place

import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.facebook.imagepipeline.request.ImageRequest
import com.ziq.base.extension.click
import com.ziq.base.extension.dp
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.coupon.CouponCampOnline
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.product.PlaceProduct
import com.ziq.zcamp.databinding.ItemPlaceDetailBookingBinding
import com.ziq.zcamp.databinding.ItemPlaceDetailBookingFooterBinding
import com.ziq.zcamp.databinding.ItemPlaceDetailBookingHeaderBinding
import com.ziq.zcamp.util.ImageUtil
import java.text.DecimalFormat

class PlaceDetailBookingGroup(val adapter: PlaceDetailAdapter) : DetailGroup() {
    override fun getHeaderViewType(groupPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_HEADER_BOOKING
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_CHILD_BOOKING
    }

    override fun getFooterViewType(groupPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_FOOTER_BOOKING
    }

    override fun getChildrenCount(): Int {
        if((productList?.size?:0) > moreLimit && !showAll){
            return moreLimit
        }
        return productList?.size?:0
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return true
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return (productList?.size?:0) > moreLimit
    }

    var couponList: List<CouponCampOnline>? = null
    var productList: MutableList<PlaceProduct>? = null
    var moreLimit = 3
    var showAll = false

    override fun onBindHeaderViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
        holder?.let {
            val binding = ItemPlaceDetailBookingHeaderBinding.bind(holder.itemView)
            val hasCoupon = couponList?.isNotEmpty() == true
            if(hasCoupon){
                binding.llCoupon.isVisible = true
                binding.rvCoupon.layoutManager = LinearLayoutManager(binding.rvCoupon.context, RecyclerView.HORIZONTAL, false)
                binding.rvCoupon.adapter = CouponAdapter(adapter.campId?:0, couponList, source = "camp_detail")
            } else {
                binding.llCoupon.isVisible = false
            }
        }
    }




    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {

        val layoutParams = holder?.itemView?.layoutParams
        if (layoutParams != null && layoutParams is StaggeredGridLayoutManager.LayoutParams) {
            layoutParams.isFullSpan = true
        }

        holder?.let {
            val binding = ItemPlaceDetailBookingBinding.bind(holder.itemView)
            if(childPosition == 0){
                binding.container.setPadding(0, 16.dp, 0, 20.dp)
            } else {
                binding.container.setPadding(0, 0, 0, 20.dp)
            }
            if(((productList?.size?:0) > moreLimit)){
                binding.container.setBackgroundResource(R.drawable.bg_white_0_0_0_0)
            } else if((childPosition == (productList?.size?:0) - 1)) {
                binding.container.setBackgroundResource(R.drawable.bg_white_0_0_5_5)
            } else {
                binding.container.setBackgroundResource(R.drawable.bg_white_0_0_0_0)
            }

            val item = productList?.get(childPosition)
            item?.let {
                if(item.goods_url_list?.isNotEmpty() == true){
                    val imageUrl = item.goods_url_list?.get(0)
                    ImageUtil.updateImageFromNetwork(binding.image, imageUrl?.url, ImageRequest.CacheChoice.DEFAULT)
                } else {
                }

                binding.name.text = item.name
                binding.description.text = item.title
                if((item.price_range?.size?:0) > 1){
                    val li = item.price_range?.sortedBy { it }
                    binding.price.text = "${DecimalFormat("0.00").format((li?.first()?:0) / 100f)}"
                }else {
                    binding.price.text = "${DecimalFormat("0.00").format((item.price?:0) / 100f)}"
                }

                if(item.is_sell_out == true){
                    binding.name.isEnabled = false
                    binding.priceSymbol.isEnabled = false
                    binding.price.isEnabled = false
                    binding.btnBook.isEnabled = false
                    binding.btnBook.isClickable = false
                    binding.btnBook.text = "已告罄"
                } else {
                    binding.name.isEnabled = true
                    binding.priceSymbol.isEnabled = true
                    binding.price.isEnabled = true
                    binding.btnBook.isEnabled = true
                    binding.btnBook.isClickable = false
                    binding.btnBook.text = "预订"
                }
//                if((item.sales_volume?:0) > 999){
//                    binding.tvBookSellInfo.text = "近30天售出999+"
//                } else {
//                    binding.tvBookSellInfo.text = "近30天售出${item.sales_volume?:0}"
//                }

                binding.root.click {
                    adapter.placeProductActionListener?.onProductClick(item)
                }
            }
        }
    }

    override fun onBindFooterViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
        holder?.let {
            val binding = ItemPlaceDetailBookingFooterBinding.bind(holder.itemView)
            updateCampParametersListUI(binding)
            holder.itemView.click {
                showAll = !showAll
                if(showAll){
                }
                adapter.notifyDataChanged()
            }
        }
    }

    fun updateCampParametersListUI(binding : ItemPlaceDetailBookingFooterBinding){
            if(showAll){
                binding.tvCampDetailProductMoreOrLess.text = "收起"
                binding.ivCampDetailProductMoreOrLess.setImageResource(R.drawable.icon_parameter_close)
            } else {
                binding.tvCampDetailProductMoreOrLess.text = "展开更多"
                binding.ivCampDetailProductMoreOrLess.setImageResource(R.drawable.icon_parameter_open)
            }
    }
}